"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.put = exports.post = exports.get = exports.controller = exports.rootRouter = exports.register = void 0;
const koa_router_1 = __importDefault(require("koa-router"));
const koa_body_1 = __importDefault(require("koa-body"));
const fs_1 = __importDefault(require("fs"));
/**
 * 二次封装koa框架，使之支持注解
 */
const rootRouter = new koa_router_1.default();
exports.rootRouter = rootRouter;
function get(n, ...i) {
    if (typeof n == "string") {
        return (target, methodName, descriptor) => {
            let url = n ? "/" + n : '';
            i.forEach((ele) => {
                url += "/:" + ele;
            });
            if (!target.router) {
                target.router = new koa_router_1.default();
            }
            target.router.get(url, async (ctx) => {
                try {
                    ctx.body = await descriptor.value(ctx.params, ctx.state);
                }
                catch (error) {
                    ctx.body = { status: -1, message: error.message };
                }
            });
        };
    }
    else if (typeof n == "undefined") {
        return (target, methodName, descriptor) => {
            let url = '/';
            if (!target.router) {
                target.router = new koa_router_1.default();
            }
            target.router.get(url, async (ctx) => {
                try {
                    ctx.body = await descriptor.value(ctx.params, ctx.state);
                }
                catch (error) {
                    ctx.body = { status: -1, message: error.message };
                }
            });
        };
    }
}
exports.get = get;
//post访问之注解 
//name 访问的地址关键字
function post(name) {
    return (target, methodName, descriptor) => {
        let url = "/" + (name ? name : '');
        if (!target.router) {
            target.router = new koa_router_1.default();
        }
        target.router.post(url, koa_body_1.default(), async (ctx) => {
            try {
                ctx.body = await descriptor.value(ctx.request.body, ctx.state);
            }
            catch (error) {
                ctx.body = { status: -1, message: error.message };
            }
        });
    };
}
exports.post = post;
//put访问之注解 
//name 访问的地址关键字
function put(name) {
    return (target, methodName, descriptor) => {
        let url = "/" + (name ? name : '');
        if (!target.router) {
            target.router = new koa_router_1.default();
        }
        target.router.put(url, koa_body_1.default(), async (ctx) => {
            try {
                ctx.body = await descriptor.value(ctx.request.body, ctx.state);
            }
            catch (error) {
                ctx.body = { status: -1, message: error.message };
            }
        });
    };
}
exports.put = put;
//路由注册以及路由路径完善
function controller(path) {
    return (target) => {
        const url = "/" + (path ? path : target.name);
        if (!target.prototype.router) {
            target.prototype.router = new koa_router_1.default();
        }
        target.prototype.router.prefix(url);
        rootRouter.use(target.prototype.router.routes());
    };
}
exports.controller = controller;
/**
 * 注册路由
 * @param path 根路径“__dirname”
 * @param conpath 控制器目录名称
 */
function register(path, conpath) {
    const p = `${path}/${conpath}`;
    getFiles(p);
}
exports.register = register;
function getFiles(path) {
    fs_1.default.readdir(path, { withFileTypes: true }, function (err, files) {
        for (let i = 0; i < files.length; i++) {
            const file = files[i];
            if (file.isDirectory()) {
                getFiles(`${path}/${file.name}`);
            }
            else {
                require(`${path}/${file.name}`);
            }
        }
    });
}
